package com.controller.grade;
import com.model.Grade;
import com.servlet.GradeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "GradeAddServlet",urlPatterns = "/grade/add")
public class AddServlet extends HttpServlet {
    private GradeService gradeService;
    public  void init() throws  ServletException{
        super.init();
        gradeService= GradeService.instance();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String name=request.getParameter("name");
       System.out.println(name);
       String desc=request.getParameter("desc");
        System.out.println(desc);
        Grade grade=new Grade(name,desc);
        try {
            gradeService.add(grade);
            response.sendRedirect(request.getContextPath()+"/grade/listenter");
        } catch (Exception e) {
         throw  new ServletException(e);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/grade/add.jsp").forward(request,response);
    }
}
